<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>画板</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <canvas id="canvas" width="100" height="100"></canvas>
    <script>
      let canvas = document.getElementById("canvas");
      canvas.width = document.documentElement.clientWidth
      canvas.height = document.documentElement.clientHeight
      let ctx = canvas.getContext("2d");

      let painting = false;
      let lastPosition;
      ctx.lineWidth = 9;
      ctx.fillStyle = "black";
      ctx.strokeStyle = 'none';
      ctx.lineCap = 'round';

      const isTouchDevice = 'ontouchstart' in window;

      function drawLine (x1,y1,x2,y2) {
        ctx.beginPath();
        ctx.moveTo(x1, y1);
        ctx.lineTo(x2, y2);
        ctx.stroke();
      }

      if (isTouchDevice) {
        canvas.ontouchstart = (e) => {
          // console.log(e)
          painting = true;
          lastPosition = [e.touches[0].clientX, e.touches[0].clientY];
        }
        canvas.ontouchmove = (e) => {
          drawLine(lastPosition[0], lastPosition[1], e.touches[0].clientX, e.touches[0].clientY)
          lastPosition = [e.touches[0].clientX, e.touches[0].clientY];
        }
      } else {
        canvas.onmousedown = (e) => {
        painting = true;
        lastPosition = [e.clientX, e.clientY];
      }

      canvas.onmousemove = (e) => {
        if (painting === true) {
            drawLine(lastPosition[0], lastPosition[1], e.clientX, e.clientY)
            lastPosition = [e.clientX, e.clientY];
        } else {
            console.log('什么都不做')
        }
      }

      canvas.onmouseup = () => {
          painting = false
      }
    }

        


    </script>
</body>
